1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
// app/api/evaluation/attachments/[id]/route.ts
import { NextRequest, NextResponse } from "next/server";
import db from "@/db/db";
import { reviewerEvaluationAttachments } from "@/db/schema";
import { eq } from "drizzle-orm";
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
import { deleteFile } from "@/lib/file-stroage";
interface Context {
params: {
id: string;
};
}
// 첨부파일 삭제
export async function DELETE(
request: NextRequest,
{ params }: Context
) {
try {
// 인증 확인
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json(
{ success: false, error: "인증이 필요합니다." },
{ status: 401 }
);
}
const attachmentId = parseInt(params.id);
if (isNaN(attachmentId)) {
return NextResponse.json(
{ success: false, error: "유효하지 않은 첨부파일 ID입니다." },
{ status: 400 }
);
}
const result = await db.transaction(async (tx) => {
// 1. 첨부파일 정보 조회
const attachment = await tx
.select({
id: reviewerEvaluationAttachments.id,
publicPath: reviewerEvaluationAttachments.publicPath,
uploadedBy: reviewerEvaluationAttachments.uploadedBy,
})
.from(reviewerEvaluationAttachments)
.where(eq(reviewerEvaluationAttachments.id, attachmentId))
.limit(1);
if (attachment.length === 0) {
throw new Error("첨부파일을 찾을 수 없습니다.");
}
// 2. 권한 확인 (업로드한 사용자만 삭제 가능)
if (attachment[0].uploadedBy !== parseInt(session.user.id)) {
throw new Error("삭제 권한이 없습니다.");
}
// 3. DB에서 첨부파일 정보 삭제
await tx
.delete(reviewerEvaluationAttachments)
.where(eq(reviewerEvaluationAttachments.id, attachmentId));
// 4. 물리적 파일 삭제
try {
await deleteFile(attachment[0].publicPath);
} catch (fileError) {
console.warn("물리적 파일 삭제 실패:", fileError);
// 파일 삭제 실패는 DB 삭제를 롤백하지 않음
}
return true;
});
return NextResponse.json({
success: true,
message: "첨부파일이 삭제되었습니다.",
});
} catch (error) {
console.error("첨부파일 삭제 실패:", error);
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : "첨부파일 삭제 중 오류가 발생했습니다.",
},
{ status: 500 }
);
}
}
// 특정 첨부파일 정보 조회
export async function GET(
request: NextRequest,
{ params }: Context
) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json(
{ success: false, error: "인증이 필요합니다." },
{ status: 401 }
);
}
const attachmentId = parseInt(params.id);
if (isNaN(attachmentId)) {
return NextResponse.json(
{ success: false, error: "유효하지 않은 첨부파일 ID입니다." },
{ status: 400 }
);
}
const attachment = await db
.select({
id: reviewerEvaluationAttachments.id,
originalFileName: reviewerEvaluationAttachments.originalFileName,
publicPath: reviewerEvaluationAttachments.publicPath,
fileSize: reviewerEvaluationAttachments.fileSize,
mimeType: reviewerEvaluationAttachments.mimeType,
description: reviewerEvaluationAttachments.description,
createdAt: reviewerEvaluationAttachments.createdAt,
})
.from(reviewerEvaluationAttachments)
.where(eq(reviewerEvaluationAttachments.id, attachmentId))
.limit(1);
if (attachment.length === 0) {
return NextResponse.json(
{ success: false, error: "첨부파일을 찾을 수 없습니다." },
{ status: 404 }
);
}
return NextResponse.json({
success: true,
attachment: attachment[0],
});
} catch (error) {
console.error("첨부파일 조회 실패:", error);
return NextResponse.json(
{
success: false,
error: "첨부파일 조회 중 오류가 발생했습니다.",
},
{ status: 500 }
);
}
}
|